package xyz.xuminghai.javafx_learn.scene.effect;

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.effect.DisplacementMap;
import javafx.scene.effect.FloatMap;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * 2023/7/7 0:17 星期五<br/>
 * 位移贴图效果 // TODO（没搞懂）
 *
 * @author xuMingHai
 */
public class DisplacementMapApp extends Application {
    private final BorderPane root = new BorderPane();

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        Text text = new Text("DisplacementMap");
        text.setFont(Font.font(50.0));
        text.setFill(Color.TOMATO);

        // 基于float数组，每4个一个单位
        Bounds layoutBounds = text.getLayoutBounds();
        FloatMap floatMap = new FloatMap((int) layoutBounds.getWidth(), (int) layoutBounds.getHeight());

        for (int i = 0; i < floatMap.getWidth(); i++) {
            for (int j = 0; j < floatMap.getHeight(); j++) {
                floatMap.setSamples(i, j, 0.0F, 0.0F, 0.0F, 1000F);
            }
        }

//        DisplacementMap displacementMap = new DisplacementMap(floatMap);
        DisplacementMap displacementMap = new DisplacementMap();
        displacementMap.setScaleX(2.0);
        displacementMap.setOffsetX(0.1);


        text.setEffect(displacementMap);

        root.setCenter(text);
    }


    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
